#include "authroledlg.h"
#include "ui_authroledlg.h"

#include <QCheckBox>
#include <QNetworkReply>

AuthRoleDlg::AuthRoleDlg(int userId,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AuthRoleDlg)
{
    ui->setupUi(this);
    this->userId = QString::number(userId);
    ui->lineEdit_gotoPage->setValidator(new QIntValidator(ui->lineEdit_gotoPage));
    tableColumn<<"序号"<<"角色编号"<<"角色名称"<<"权限字符"<<"创建时间";
    init();
}

AuthRoleDlg::~AuthRoleDlg()
{
    delete ui;
}

void AuthRoleDlg::init()
{
    getUserAndRoles();
    for(int i = 0;i< user->roles.size();i++){
        roleIds.append(QString::number(user->roles.at(i).roleId));
    }
    ui->lineEdit_username->setText(user->userName);
    ui->lineEdit_nkckname->setText(user->nickName);
    count = roles.size();
    ui->label_count->setText("共" + QString::number(count) + "条");
    setTableWidget();

}

void AuthRoleDlg::getUserAndRoles()
{
    QNetworkReply *r = userManagerApi.authRoleInfo(userId);
    QByteArray bytes = r->readAll().data();
    if(bytes.isEmpty()){
        QMessageBox::warning(this,"提示","服务器连接失败");
        return;
    }
    QJsonObject obj = QJsonDocument::fromJson(bytes).object();
    if(200 != obj.value("code").toInt()){
        QMessageBox::warning(this,"提示",obj.value("msg").toString());
        return;
    }
    QJsonArray roleArr = obj.value("roles").toArray();
    for(int i = 0;i< roleArr.size();i++){
        roles.append(utils.getRole(roleArr.at(i).toObject()));
    }
    user = utils.getUserByJsonObj(obj.value("user").toObject());
}

void AuthRoleDlg::on_comboBox_currentIndexChanged(int index)
{
    switch (index) {
    case 0:
        pageSize = 10;
        break;
    case 1:
        pageSize = 20;
        break;
    case 2:
        pageSize = 50;
        break;
    case 3:
        pageSize = 100;
        break;
    default:
        break;
    }
    currentPage = 1;
    setTableWidget();
}

void AuthRoleDlg::setTableWidget()
{
    ui->tableWidget->setSortingEnabled(0);
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(tableColumn.size());
    ui->tableWidget->setHorizontalHeaderLabels(tableColumn);
    ui->tableWidget->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    ui->tableWidget->setColumnWidth(4,140);
    if(pageSize * currentPage > count){
        ui->tableWidget->setRowCount(count - (pageSize*(currentPage - 1)));
    }else{
        ui->tableWidget->setRowCount(pageSize);
    }
    for(int i = (currentPage-1) * 10;i< pageSize * currentPage;i++){
        if(i > roles.size() - 1){
            break;
        }
        QCheckBox * b = new QCheckBox;
        QWidget *w = new QWidget;
        QHBoxLayout *l = new QHBoxLayout(w);
        l->addWidget(b);
        l->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
        ui->tableWidget->setCellWidget(i%pageSize,0,w);
        connect(b,&QCheckBox::clicked,this,[=](){
            if(b->isChecked()){
                roleIds.append(QString::number(roles.at(i).roleId));
            }else{
                roleIds.removeOne(QString::number(roles.at(i).roleId));
            }
        });
        //选中当前用户拥有的角色
        for(int j = 0;j< roleIds.size();j++){
            if(roleIds.at(j).toInt() == roles.at(i).roleId){
                b->setChecked(1);
            }
        }
        ui->tableWidget->setItem(i%pageSize,1,new QTableWidgetItem(QString::number(roles.at(i).roleId)));
        ui->tableWidget->setItem(i%pageSize,2,new QTableWidgetItem(roles.at(i).roleName));
        ui->tableWidget->setItem(i%pageSize,3,new QTableWidgetItem(roles.at(i).roleKey));
        ui->tableWidget->setItem(i%pageSize,4,new QTableWidgetItem(roles.at(i).createTime));
        ui->tableWidget->item(i%pageSize,1)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->item(i%pageSize,2)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->item(i%pageSize,3)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        ui->tableWidget->item(i%pageSize,4)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    }
    ui->toolButton->setEnabled(1);
    if(currentPage == 1){
        ui->toolButton->setEnabled(0);
    }
    if((count - pageSize * currentPage) < 1){
        ui->btn_nextPage->setEnabled(0);
    }else{
        ui->btn_nextPage->setEnabled(1);
    }
    ui->tableWidget->setSortingEnabled(1);
}

void AuthRoleDlg::on_lineEdit_gotoPage_returnPressed()
{
    int totalPage = count / pageSize + 1;
    int linePage = ui->lineEdit_gotoPage->text().toInt();
    if(totalPage < linePage){
        return;
    }
    ui->lineEdit_gotoPage->clear();
    ui->label_currentPage->setText(QString::number(linePage));
    currentPage = linePage;
    setTableWidget();
}


void AuthRoleDlg::on_toolButton_clicked()
{
    currentPage--;
    ui->toolButton->setEnabled(0);
    ui->label_currentPage->setText(QString::number(currentPage));
    setTableWidget();
}


void AuthRoleDlg::on_btn_nextPage_clicked()
{
    ui->btn_nextPage->setEnabled(0);
    currentPage ++;
    ui->label_currentPage->setText(QString::number(currentPage));
    setTableWidget();
}


void AuthRoleDlg::on_btn_close_clicked()
{
    this->close();
}


void AuthRoleDlg::on_btn_submit_clicked()
{
    userRoleIds.clear();
    for(int i = 0;i< roleIds.size();i++){
        userRoleIds.append(roleIds.at(i) + ",");
    }
    userRoleIds.chop(1);
    QNetworkReply *r = userManagerApi.authRole(userId,userRoleIds);
    QByteArray bytes = r->readAll().data();
    if(bytes.isEmpty()){
        QMessageBox::warning(this,"提示","服务器连接失败");
        return;
    }
    QJsonObject obj = QJsonDocument::fromJson(bytes).object();
    QMessageBox::warning(this,"提示",obj.value("msg").toString());
    if(200 != obj.value("code").toInt()){
        return;
    }
    this->close();
}

